package mn.idax.exchange.otc.service.impl;

import com.alibaba.fastjson.JSON;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.internal.Mimetypes;
import com.aliyun.oss.model.*;
import mn.idax.exchange.otc.config.AliyunOSSConfig;
import mn.idax.exchange.otc.domain.ChatFileDTO;
import mn.idax.exchange.otc.enums.RedisPrefixKeyEnum;
import mn.idax.exchange.otc.enums.response.AliyunOSSResponseEnum;
import mn.idax.exchange.otc.exception.IdaxException;
import mn.idax.exchange.otc.service.AliyunOSSService;
import mn.idax.exchange.otc.util.AliyunOSSUtils;
import mn.idax.exchange.otc.util.LogUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Auther: dison
 * @Date: 2018/8/6 19:29
 * @Description:
 *           阿里云 OSS 服务层
 *              <b>注意</>：
 *                      1.图片可缩放，直接在对应链接加入参数即可，无需额外添加代码
 *                                               (1).参考地址:https://helpcdn.aliyun.com/document_detail/44695.html?spm=a2c4g.11186623.2.8.mqmTnI
 *                                               (2).demo:http://otcpublic.oss-cn-hangzhou.aliyuncs.com/1533620387869.png?x-oss-process=image/circle,r_3000
 *                      2.每个附件可以设置过期时间，示例： ossClient.generatePresignedUrl(bucketName, objectName, expiration);
 *                                              (1).需将空间设置为私有:ossClient.setBucketAcl(ossConfig.getBucketName(), CannedAccessControlList.PublicRead);
 *                                              (2).再生成签名访问链接，必须设置过期时间。且过期时间最大为18小时
 *                      3.获取MIMEType:Mimetypes.getInstance().getMimetype(fileName);
 */
@Service
public class AliyunOSSServiceImpl implements AliyunOSSService {

    @Autowired
    private AliyunOSSConfig ossConfig;
    @Autowired
    private RedisService redisService;

    @Override
    public String uploadFile(File file, String fileName) {
        OSSClient ossClient = AliyunOSSUtils.getOSSClient(ossConfig);
        return AliyunOSSUtils.uploadFile(ossClient,ossConfig,fileName,file);
    }

    @Override
    public String partUploadInputStream(File file, String fileName, Long fileLength) {
        OSSClient ossClient = AliyunOSSUtils.getOSSClient(ossConfig);
        return AliyunOSSUtils.partUploadInputStream(ossClient,ossConfig,fileName,fileLength,file);
    }

    @Override
    public String uploadStream(InputStream inputStream,String fileName){
        long start = System.currentTimeMillis();

        OSSClient ossClient = AliyunOSSUtils.getOSSClient(ossConfig);
        String s = AliyunOSSUtils.uploadInputStream(ossClient, ossConfig, fileName, inputStream);

        long end = System.currentTimeMillis();
        long cost = end - start;
        LogUtils.info(AliyunOSSUtils.class,"AliyunUpload uploadStream cost " + cost);
        return s;
    }

    @Override
    public String uploadChat(InputStream inputStream, String fileName) {
        String fullFileName = this.uploadStream(inputStream,fileName);
        String imageKey = AliyunOSSUtils.getImageKey(fullFileName);
        ChatFileDTO chatFile = new ChatFileDTO();
        chatFile.setName(fileName);
        chatFile.setSource(fullFileName);

        this.redisService.boundHashOps(RedisPrefixKeyEnum.chat_file_upload_key.getKey(),imageKey, JSON.toJSONString(chatFile));

        return imageKey;
    }
}
